from django.urls import path
from ninja.errors import ValidationError, AuthenticationError, HttpError
from ninja import NinjaAPI
from django.http import HttpResponse, JsonResponse

from utils.ninja import MyNinjaAPI, format_error_msg
from apps.robotControl.api import router as robot_control_router

api = MyNinjaAPI(
    title="API接口文档",
    description="这是文档描述",
)


# 重写验证错误的返回
@api.exception_handler(ValidationError)
def validation_errors(request, exc):
    error_lst = exc.errors
    data = None
    msg = format_error_msg(error_lst)
    return api.create_response(request, data, status=422, msg=msg, code=422, success=False)


api.add_router("/", robot_control_router, tags=['机器人操作相关接口'])

urlpatterns = [
    path("api/", api.urls),
]
